package com.ruoyi.project.xkx.order.service.impl;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.project.xkx.order.OrderDetailsService;
import com.ruoyi.project.xkx.order.domain.OrderDetails;
import com.ruoyi.project.xkx.order.domain.OrderWay;
import com.ruoyi.project.xkx.order.domain.YsOrder;
import com.ruoyi.project.xkx.order.mapper.YsOrderMapper;
import com.ruoyi.project.xkx.order.service.OrderWayService;
import com.ruoyi.project.xkx.order.service.YsOrderService;
import com.ruoyi.project.xkx.school.domain.XkxCourse;
import com.ruoyi.project.xkx.school.mapper.XkxCourseMapper;
import com.ruoyi.project.xkx.school.service.IXkxCourseService;
import com.ruoyi.project.xkx.school.service.IXkxSchoolCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author WuYalong
 * @since 2021-05-10
 */
@Service
public class YsOrderServiceImpl extends ServiceImpl<YsOrderMapper, YsOrder> implements YsOrderService {
    @Autowired
    private YsOrderMapper ysOrderMapper;//订单业务类
    @Autowired
    private OrderDetailsService orderDetailsService; //订单详情业务类
    @Autowired
    private IXkxCourseService iXkxCourseService;//课程业务类
    @Autowired
    private OrderWayService orderWayService;
    @Autowired
    private XkxCourseMapper xkxCourseMapper;

    @Override
    public Page<YsOrder> getListByPage(Page<YsOrder> page,String orderNumber) {
        return ysOrderMapper.getListByPage(page,orderNumber);
    }
    /**
     * 创建订单 生成详情
     * io操作 必须开启事物 同时成功 同时失败
     * @param ysOrder
     * @return
     */
    @Transactional
    @Override
    public boolean addNewOrderAndGenerateDetails(YsOrder ysOrder) {
        if (ysOrder!=null){
            //1 添加一个订单 收集数据
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String time = sdf.format(new Date());//将当前系统时间转换为字符串
            String response = time.replaceAll("[[\\s-:punct:]]","");//将时间中的"- 空格 /"删除,组成一个新字符串

            String preString = "YS";//通过StringBuffer进行追加
            String orderNumber = preString+response;
            //2 创建对象设置值
            YsOrder ysOrder1 = new YsOrder();
            ysOrder1.setOrderNumber(orderNumber);//设置订单编号
            ysOrder1.setOrderDate(new Date());
            ysOrder1.setOrderCategoryId(ysOrder.getOrderCategoryId());//设置订单类型
            ysOrder1.setOrderAmount(ysOrder.getOrderAmount());//设置订单金额
            ysOrder1.setWayId(ysOrder.getWayId());//设置支付方式
            ysOrder1.setWayAmount(ysOrder.getWayAmount());//设置支付金额 模拟支付
            ysOrder1.setStudentId(ysOrder.getStudentId());//设值学生姓名
            ysOrder1.setUserId(ysOrder.getUserId());//设置顾问
            ysOrder1.setCourseId(ysOrder.getCourseId());//设置课程顾问
            ysOrderMapper.insert(ysOrder1);//添加订单

            //1 拿到订单中的每个关系id
            Integer courseId = ysOrder.getCourseId();//课程id 查课程表得到课程名称和课时数 插入详情
            Integer wayId = ysOrder.getWayId();//顶单支付方式 查询支付方式表 得到 支付方式 插入详情
            Double wayAmount = ysOrder.getWayAmount();//支付金额 dy
            //2 根据关系id查询出来要插入到详情表中的数据

            Object course = xkxCourseMapper.selectXkxCourseById(courseId);//课程对象 课程名称和课时数
            XkxCourse courseObj = (XkxCourse) course;
            String courseName = courseObj.getCourseName();//课程名称
            Integer courseHours = courseObj.getCourseHour();//课时数
            Object way = orderWayService.getById(wayId);//支付方式对象
            OrderWay wayObj = (OrderWay) way;
            String wayName = wayObj.getWayName();//支付方式名称
            // 创建订单详情对象
            OrderDetails details = new OrderDetails();
            details.setOrderNumber(orderNumber);//设置订单编号 把系统时间处理过的 订单编号
            details.setCourseName(courseName);
            details.setCourseHours(courseHours);
            details.setWayName(wayName);//设置支付方式
            details.setWayAmount(wayAmount);
            orderDetailsService.save(details);
            if(details==null){
                return false;
            }
        }
        return false;
    }
}
